<?php
include('./lib/phpunit.php');

include('../../commonvalidate/php/validate_configure.php');
include('../../commonvalidate/php/validate_func.php');
include('../../commonvalidate/php/validate_util.php');

include('../../sample1/validate_config.php');

class ValidateFuncTest extends TestCase {
	
	var $configure; 
	
	function test_cv_checkIsNull() {
		
		global $COMMON_VALIDATE_CONFIG;
		
		$context = array();
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		
		$validateFunc = new ValidateFunc($this->configure);
		
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNull("test", "advice_test", "TEST", null, $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST不能为空。", $error['error_msg']);
		
		
		$context = array();
		$context['test'] = 'abc';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		
		$validateFunc = new ValidateFunc($this->configure);
		
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNull("test", "advice_test", "TEST", null, $error_list);
		
		$this->assertEquals(0, count($error_list));
	}
	
	function test_cv_checkLengthRange() {
		global $COMMON_VALIDATE_CONFIG;
		
		$context = array();
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkLengthRange("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST的长度必须在1和10之间。", $error['error_msg']);

		$context = array();
		$context['test'] = 'abc';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkLengthRange("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(0, count($error_list));
		
		$context = array();
		$context['test'] = 'abababababab';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkLengthRange("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST的长度必须在1和10之间。", $error['error_msg']);
	}
	
	function test_cv_checkIsNumber() {
		
		global $COMMON_VALIDATE_CONFIG;
		
		$context = array();
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNumber("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST不是有效的数字。", $error['error_msg']);
		
		$context = array();
		$context['test'] = '123ab';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNumber("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST不是有效的数字。", $error['error_msg']);
		
		$context = array();
		$context['test'] = '123';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNumber("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(0, count($error_list));	
		
		$context = array();
		$context['test'] = '123.45';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNumber("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(0, count($error_list));	
		
		$context = array();
		$context['test'] = 123.45;
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsNumber("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(0, count($error_list));		
	}
	
	function test_cv_checkIsInt() {
		
		global $COMMON_VALIDATE_CONFIG;
		$context = array();
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);	
		
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsInt("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST必须是整数。", $error['error_msg']);
		
		$context = array();
		$context['test'] = '12ab';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);	
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsInt("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST必须是整数。", $error['error_msg']);
		
		$context = array();
		$context['test'] = '12.12';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsInt("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(1, count($error_list));
		$error = $error_list[0];
		$this->assertEquals("advice_test", $error['advice_id']);
		$this->assertEquals("TEST必须是整数。", $error['error_msg']);
		
		$context = array();
		$context['test'] = 12;
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsInt("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(0, count($error_list));
		
		$context = array();
		$context['test'] = '12';
		$this->configure = new ValidateConfigure($COMMON_VALIDATE_CONFIG, $context);
		$validateFunc = new ValidateFunc($this->configure);
		$error_list = array();
		$error_list = $validateFunc->cv_checkIsInt("test", "advice_test", "TEST", array(1, 10), $error_list);
		
		$this->assertEquals(0, count($error_list));
	}
	
}

$suite = new TestSuite();
$suite->addTest(new TestSuite("ValidateFuncTest"));

?>
